using System;
using System.Data;
using System.Collections;
using Castle.Facilities.NHibernateIntegration;
using Castle.Services.Transaction;
using NHibernate;

using BEGDC.CC.Entity;

namespace BEGDC.CC.BL.Email
{
	/// <summary>
	/// Summary description for CODMEmailProxy.
	/// </summary>
	[Transactional]
	public class CCEmailProxy : IEmail
	{
		private ISessionManager sessionManager;

		public CCEmailProxy()
		{
			
			//
			// TODO: Add constructor logic here
			//
		}
		public CCEmailProxy(ISessionManager sessionManager)
		{
			this.sessionManager = sessionManager;
		}
		#region IEmail Members

		[Transaction(TransactionMode.Requires)]
		public void Send(int appID, string templateKey)
		{
			using( ISession session = sessionManager.OpenSession() )
			{
				try
				{
					
					BEGDC.CC.BL.Common.Query.IQuery iquery = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as BEGDC.CC.BL.Common.Query.IQuery;
			
					BEGDC.CC.Entity.Application application = iquery.GetAppById(appID);

					BEGDC.CC.Entity.Email email = new BEGDC.CC.Entity.Email();
					email.ApID=0;
					email.Application = application;
					email.TemplateName = templateKey;
					email.IsSend = false;
					session.Save( email );
				}
				catch(HibernateException hex)
				{
					throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.DB_Exception, hex.Message );
				}
				catch(Exception ex)
				{
					throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.EmailService_Exception, ex.Message );
				}
				finally
				{
					session.Close();
				}
			}
		}


		//<--joan 2007/09/17
		[Transaction(TransactionMode.Requires)]
		public void Send(int appID, int apID, string templateKey)
		{
			using( ISession session = sessionManager.OpenSession() )
			{
				try
				{
					
					BEGDC.CC.BL.Common.Query.IQuery iquery = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as BEGDC.CC.BL.Common.Query.IQuery;
			
					BEGDC.CC.Entity.Application application = iquery.GetAppById(appID);

					BEGDC.CC.Entity.Email email = new BEGDC.CC.Entity.Email();
					email.ApID=apID;
					email.Application = application;
					email.TemplateName = templateKey;
					email.IsSend = false;
					session.Save( email );
				}
				catch(HibernateException hex)
				{
					throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.DB_Exception, hex.Message );
				}
				catch(Exception ex)
				{
					throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.EmailService_Exception, ex.Message );
				}
				finally
				{
					session.Close();
				}
			}
		}

		//-->end

		[Transaction(TransactionMode.Requires)]
		public void Send(long flowID, string templateKey)
		{
			using( ISession session = sessionManager.OpenSession() )
			{
				try
				{
					BEGDC.CC.BL.Common.Query.IQuery iquery = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as BEGDC.CC.BL.Common.Query.IQuery;
			
					BEGDC.CC.Entity.FlowInstance flow = iquery.QueryFlowInstanceByFlowId(Convert.ToInt32(flowID));
					if (flow != null)
					{
						BEGDC.CC.Entity.Application application = flow.Application;

						BEGDC.CC.Entity.Email email = new BEGDC.CC.Entity.Email();
						email.ApID=0;
						email.Application = application;
						email.TemplateName = templateKey;
						email.IsSend = false;
						session.Save( email );

						return;
					}
					else
					{
						string sql = " select appid from vw_APInfo where flowid = " + flowID;
						System.Data.DataTable dtAP = BEGDC.Utility.NHibernate.NHibernateUtil.Execute(session,sql);
						if (dtAP.Rows.Count > 0)
						{
							string appid = dtAP.Rows[0][0].ToString();
							
							BEGDC.CC.Entity.Email email = new BEGDC.CC.Entity.Email();
							BEGDC.CC.Entity.Application application = new Application();
							application.Id = Convert.ToInt32(appid);
							email.Application = application;
							email.TemplateName = templateKey;
							email.IsSend = false;
							session.Save( email );
						}
					}

				}
				catch(HibernateException hex)
				{
//					throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.DB_Exception, hex.Message );
				}
				catch(Exception ex)
				{
//					throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.EmailService_Exception, ex.Message );
				}
				finally
				{
					session.Close();
				}
			}
		}

		[Transaction(TransactionMode.Requires)]
		public void Send(BEGDC.CC.Entity.Application application, string templateKey)
		{
			if( application == null )
				throw new BEGDC.Utility.ExceptionManagement.GDCException(Common.ErrorCode.Null_Exception, "projectId of email undefined");
			
			BEGDC.CC.Entity.Email email = new BEGDC.CC.Entity.Email();
			email.ApID=0;
			email.Application = application;
			email.TemplateName = templateKey;
			email.IsSend = false;
			
			using( ISession session = sessionManager.OpenSession() )
			{
				try
				{
					session.Save( email );
				}
				catch(HibernateException hex)
				{
					throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.DB_Exception, hex.Message );
				}
				catch(Exception ex)
				{
					throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.EmailService_Exception, ex.Message );
				}
				finally
				{
					session.Close();
				}
			}
		}


		[Transaction(TransactionMode.Requires)]
		public IList GetEmailList()
		{
			IList list = null;
			using( ISession session = sessionManager.OpenSession() )
			{
				try
				{
					list = session.Find( "From Email WHERE IsSend = 0 " );
				}
				catch(Exception ex)
				{
					throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.EmailService_Exception, ex.Message);
				}
				finally
				{
					session.Close();
				}
				return list;
			}
		}

		[Transaction(TransactionMode.Requires)]
		public IList GetEmailList(ISession session)
		{
			IList list = null;
			
			try
			{
				list = session.Find( "From Email WHERE IsSend = 0 " );
			}
			catch(Exception ex)
			{
				System.Diagnostics.Debug.WriteLine( ex.Message );
				return null;
				//throw new BEGDC.Utility.ExceptionManagement.GDCException( Common.ErrorCode.EmailService_Exception, ex.Message);
			}
			return list;
			
		}
		#endregion
	}
}
